//
//  MonthView.m
//  MonthView
//
//  Created by  ibokan on 10-8-23.
//  Copyright 2010 tencent.com. All rights reserved.
//

#import "MonthView.h"
#import "CalendarViewController.h"
#import "Constants.h"
#import <QuartzCore/QuartzCore.h>
#import "SmartRoadShowAppDelegate.h"
#import "DefaultViewController.h"
#import "NSDate-Helper.h"
#import "MonthButton.h"

@implementation MonthView
@synthesize currentDate;

-(void) goToPreMonth
{
	SmartRoadShowAppDelegate *delegate = (SmartRoadShowAppDelegate *)[[UIApplication sharedApplication] delegate];
	[delegate._calendarViewController._scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
}

-(void) goToNextMonth
{
	SmartRoadShowAppDelegate *delegate = (SmartRoadShowAppDelegate *)[[UIApplication sharedApplication] delegate];
	[delegate._calendarViewController._scrollView setContentOffset:CGPointMake(640, 0) animated:YES];
}

- (id)initWithFrame:(CGRect)frame 
{
    if ((self = [super initWithFrame:frame])) 
	{
		//这里也必须retain一下,不然currentDate 一个周期后变成无效的值
		currentDate = [[NSDate date] retain];
		self.backgroundColor = [UIColor clearColor];
	    //self.layer.contents = (id)[UIImage imageNamed:@"calendarBG.png"].CGImage;
	
		UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
		[leftBtn setBackgroundImage:[UIImage imageNamed:@"pre.png"] forState:UIControlStateNormal];
		leftBtn.frame = CGRectMake(45, 7, 27, 27);
		leftBtn.tag = 501;
		[leftBtn addTarget:self action:@selector(goToPreMonth) forControlEvents:UIControlEventTouchUpInside];
		[self addSubview:leftBtn];
		
		UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
		rightBtn.frame = CGRectMake(235, 7, 27, 27);
		rightBtn.tag = 502;
		[rightBtn setBackgroundImage:[UIImage imageNamed:@"next.png"] forState:UIControlStateNormal];

		[rightBtn addTarget:self action:@selector(goToNextMonth) forControlEvents:UIControlEventTouchUpInside];
		[self addSubview:rightBtn];
		
		UIImageView *weekImageView = [[UIImageView alloc] initWithFrame:CGRectMake(13, 40, 286, 39)];
		weekImageView.image = [UIImage imageNamed:@"week.png"];
		weekImageView.backgroundColor = [UIColor clearColor];
		[self addSubview:weekImageView];
		[weekImageView release];
		

    }
	
    return self;
}



-(void) addViewWithDate:(NSDate *)date
{
	currentDate = [date retain];
	//返回这个月的第一天的NSdate类型;
	NSDate *beginMonth = [currentDate dateAfterDay:-[currentDate getDay] + 1];
	
	titleButton = [UIButton buttonWithType:UIButtonTypeCustom];
	titleButton.backgroundColor = [UIColor clearColor];
	[titleButton.titleLabel setFont:[UIFont boldSystemFontOfSize:16]];
	titleButton.tag = 500;
	titleButton.frame = CGRectMake(80, 7, 150, 30);
	[titleButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
	[titleButton setTitle:[NSString stringWithFormat:@"%d年%d月",[beginMonth getYear],[beginMonth getMonth]] forState:UIControlStateNormal];
	[self addSubview:titleButton];
	
	//看当前这个月横跨几周,就画几行
	int weeks = [date getWeekNumOfMonth];
		
	//看这个月的标记 -1表示前一个月  0表示这个月 1表示下个月
	int monthTag; 
	for(int i = 0; i<7 *weeks; i++)
	{
		CGRect theFrame = CGRectMake(13+41 *(i%7), 80 +38 *(i/7) , kButtonWidth, 37);
		CGRect theFrame1 = CGRectMake(13+41*(i%7), 80+38*(i/7), kButtonWidth, 37);
		
		//nowMonthDate表示现在这个view中所有的天数Date类,从星期日开始
		NSDate *nowMonthDate = [[beginMonth beginningOfWeek] dateAfterDay:i];
		if([date getYear] == [nowMonthDate getYear])
		{
			monthTag  = [date getMonth]- [nowMonthDate getMonth];
		}
		else 
		{
			monthTag = [date getYear] - [nowMonthDate getYear]; //如果不是同一年,同monthTag定义为他们的年之差
		}
		
		if(weeks <6)
		{
			//添加每一个按钮在视图上
		    MonthButton *myButton = [[MonthButton alloc] initButtonWithDate:nowMonthDate
																	  Frame:theFrame
																 isNowMonth:monthTag];
			[myButton setBackgroundColor:[UIColor clearColor]];
			myButton.layer.contents = (id)[UIImage imageNamed:@"buttonBj.png"].CGImage;
			[self addSubview:myButton];
			[myButton release];
		}
		else if(weeks == 6)
		{

			MonthButton *myButton = [[MonthButton alloc] initButtonWithDate:nowMonthDate
																	  Frame:theFrame1
																 isNowMonth:monthTag];
			[myButton setBackgroundColor:[UIColor clearColor]];
			myButton.layer.contents = (id)[UIImage imageNamed:@"buttonBj.png"].CGImage;
			[self addSubview:myButton];
			[myButton release];
		}
		
	}
	
	
}


+(MonthView *)monthViewWithDate:(NSDate *)date Frame:(CGRect) frame1
{
	MonthView *myView = [[MonthView alloc] initWithFrame:frame1];
	[myView addViewWithDate:date];
	return [myView autorelease];
	
}


- (void)dealloc
{
	[currentDate release];
    [super dealloc];
}


@end
